<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end" align="middle">
          <el-button type="primary" plain @click="addRole">添加角色</el-button>
        </el-row>
        <el-divider />
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="title"
            label="角色名称"
            align="center"
          />
          <el-table-column
            prop="description"
            label="角色描述"
            align="center"
          />
          <el-table-column
            label="操作"
            align="center"
            width="280"
          >
            <template slot-scope="{row}">
              <el-button type="warning" plain size="small" @click="assign(row.id)">分配权限</el-button>
              <el-button type="primary" plain size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" plain size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-divider />
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="limit"
            @current-change="onChange"
          />
        </el-row>
      </el-card>
      <el-dialog :title="form.id?'编辑角色':'新增角色'" :visible.sync="dialogFormVisible">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-form-item label="角色名称" style="width:90%" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="角色描述" style="width:90%" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" plain @click="closeFn">取 消</el-button>
          <el-button type="primary" size="small" plain @click="confirm">确 定</el-button>
        </div>
      </el-dialog>
      <assignPerm ref="assignPerm" :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import { getRolesList, getRolesCount, addRoles, getRole, editRole, delRole } from '@/api/roles'
import assignPerm from './components/assign-prem.vue'
export default {
  components: { assignPerm },
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      total: 0,
      limit: 5,
      start: 0,
      showDialog: false
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const res = await getRolesList({ _limit: this.limit, _start: this.start })
      // console.log(102, res)
      this.tableData = res.data
      const res1 = await getRolesCount()
      // console.log(res)
      this.total = res1.data
    },
    addRole() {
      this.dialogFormVisible = true
    },
    closeFn() {
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
      this.form = {
        title: '',
        description: ''
      }
    },
    async confirm() {
      await this.$refs.form.validate()
      if (this.form.id) {
        await editRole(this.form)
      } else {
        await addRoles(this.form)
      }
      this.getRolesList()
      this.closeFn()
    },
    async editFn(id) {
      this.dialogFormVisible = true
      const res = await getRole(id)
      // console.log(res)
      this.form = res.data
    },
    delFn(id) {
      this.$confirm('确定删除此角色吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        if (this.tableData.length === 1 && this.start >= this.limit) {
          this.start -= this.limit
        }
        this.getRolesList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async onChange(curPage) {
      this.start = (curPage - 1) * this.limit
      await this.getRolesList()
    },
    assign(id) {
      this.showDialog = true
      this.$refs.assignPerm.getRole(id)
    }
  }
}
</script>

<style>

</style>

